package pkg.tresEnRaya;

import java.awt.GridLayout;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;


	public class Tablero extends JPanel implements ActionListener {
	    private JButton b1;
	    private JButton b2;
	    private JButton b3;
	    private JButton b4;
	    private JButton b5;
	    private JButton b6;
	    private JButton b7;
	    private JButton b8;
	    private JButton b9;
	    private JLabel msg = null;
	    private int movimientos = 0;
	    private boolean seguir = true;
	    private boolean isBloqueado = false;
	    private ArrayList posibilidades = new ArrayList();
	    private boolean ordenador = true;

	    public Tablero(JLabel msg) {
	        this.msg = msg;
	        this.setLayout(new GridLayout(3, 3));
	        b1= new JButton();
	        this.b1.addActionListener(this);
	        this.add(b1);
	        
	        b2= new JButton();
	        this.b2.addActionListener(this);
	        this.add(b2);
	        
	        b3= new JButton();
	        this.b3.addActionListener(this);
	        this.add(b3);
	        
	        b4= new JButton();
	        this.b4.addActionListener(this);
	        this.add(b4);
	        
	        b5= new JButton();
	        this.b5.addActionListener(this);
	        this.add(b5);
	        
	        b6= new JButton();
	        this.b6.addActionListener(this);
	        this.add(b6);
	        
	        b7= new JButton();
	        this.b7.addActionListener(this);
	        this.add(b7);
	        
	        b8= new JButton();
	        this.b8.addActionListener(this);
	        this.add(b8);
	        
	        b9= new JButton();
	        this.b9.addActionListener(this);
	        this.add(b9);
	        
	        for(int i=0; i<9; i++)
	        {
	        	posibilidades.add(i);
	        }
	        
	    }

	    @Override
	    public void actionPerformed(ActionEvent ae) {
	        JButton source = (JButton) ae.getSource();
	        if (source.getLabel().equals("")) {
	            movimientos++;
	            if (!ordenador && !isBloqueado)
	            {
	            this.marca(source);
	            if (this.revision(source)) {
	                msg.setText("GANA " + source.getLabel() + ".");
	                this.bloquear();
	            } else if (movimientos == 9) {
	                msg.setText("Fin. Habeis quedado en Tablas");
	                this.bloquear();
	            }
	            }
	            else if (ordenador && !isBloqueado) {
	                //Nose contruir al ordenador
	            }
	        }
	    }

	    public void marca(JButton source) {
	        if (seguir) {
	            source.setLabel("X");
	            seguir = !seguir;
	        } else {
	            source.setLabel("O");
	            seguir = !seguir;
	        }
	    }

	    public void empezarDeNuevo() {
	        this.isBloqueado = false;
	        movimientos = 0;
	        posibilidades.removeAll(posibilidades);
	        
	        b1.setLabel("");
	        b1.setEnabled(true);
	        
	        b2.setLabel("");
	        b2.setEnabled(true);
	        
	        b3.setLabel("");
	        b3.setEnabled(true);
	        
	        b4.setLabel("");
	        b4.setEnabled(true);
	        
	        b5.setLabel("");
	        b5.setEnabled(true);
	        
	        b6.setLabel("");
	        b6.setEnabled(true);
	        
	        b7.setLabel("");
	        b7.setEnabled(true);
	        
	        b8.setLabel("");
	        b8.setEnabled(true);
	        
	        b9.setLabel("");
	        b9.setEnabled(true);
	        for(int i=0; i<9; i++)
	        {
	        	posibilidades.add(i);
	        }
	    }

	    public void bloquear() {
	        isBloqueado = true;
	        b1.setEnabled(false);
	        b2.setEnabled(false);
	        b3.setEnabled(false);
	        b4.setEnabled(false);
	        b5.setEnabled(false);
	        b6.setEnabled(false);
	        b7.setEnabled(false);
	        b8.setEnabled(false);
	        b9.setEnabled(false);
	    }

	    /**
	     * @return the ordenador
	     */
	    public boolean isOrdenador() {
	        return ordenador;
	    }

	    private boolean revision(JButton source) {
	        String simbolo = source.getActionCommand();

	            if (b1.getActionCommand().equals(simbolo)) {
	                if (b2.getActionCommand().equals(simbolo) && b3.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b4.getActionCommand().equals(simbolo) && b7.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b5.getActionCommand().equals(simbolo) && b9.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	            } else if (b3.getActionCommand().equals(simbolo)) {
	                if (b2.getActionCommand().equals(simbolo) && b1.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b6.getActionCommand().equals(simbolo) && b9.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b5.getActionCommand().equals(simbolo) && b7.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	            } else if (b2.getActionCommand().equals(simbolo)){
	                if (b1.getActionCommand().equals(simbolo) && b3.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b5.getActionCommand().equals(simbolo) && b8.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	            }else if (b4.getActionCommand().equals(simbolo)) {
	                if (b1.getActionCommand().equals(simbolo) && b7.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b5.getActionCommand().equals(simbolo) && b6.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	            } else if (b5.getActionCommand().equals(simbolo)){
	                if (b1.getActionCommand().equals(simbolo) && b9.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b7.getActionCommand().equals(simbolo) && b3.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b2.getActionCommand().equals(simbolo) && b8.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b4.getActionCommand().equals(simbolo) && b6.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	            }else if (b6.getActionCommand().equals(simbolo)) {
	                if (b3.getActionCommand().equals(simbolo) && b9.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b4.getActionCommand().equals(simbolo) && b5.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	            } else if (b7.getActionCommand().equals(simbolo)){
	                if (b1.getActionCommand().equals(simbolo) && b4.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b3.getActionCommand().equals(simbolo) && b5.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b9.getActionCommand().equals(simbolo) && b8.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	            }else if (b8.getActionCommand().equals(simbolo)) {
	                if (b2.getActionCommand().equals(simbolo) && b5.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b7.getActionCommand().equals(simbolo) && b9.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	            } else if (b9.getActionCommand().equals(simbolo)){
	                if (b3.getActionCommand().equals(simbolo) && b6.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b7.getActionCommand().equals(simbolo) && b8.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	                if (b1.getActionCommand().equals(simbolo) && b5.getActionCommand().endsWith(simbolo)) {
	                    return true;
	                }
	            }
	           
	        return false;
	    }

	    /**
	     * @param ordenador the ordenador to set
	     */
	    public void setOrdenador(boolean ordenador) {
	        this.ordenador = ordenador;
	    }
	}